﻿<?php

class Uas extends CI_Controller
{
	function __construct()
    {
        parent::__construct();
		$this->load->library('session');
		$this->load->helper('url');
	}
	
	public function Login()
	{
		$this->load->model('socios_model');
		$socio = $this->socios_model->getByDni($this->input->post('dni'));
		if($socio == null)
		echo "No existe ese usuario";
		else
		{
			if($socio->password == $this->input->post('password'))
			{
				$socioLogueado = array(
                   'dni'  => $socio->dni,
                   'nombre'     => $socio->nombre,
                   'apellido' => $socio->apellido,
				   'admin' => $socio->admin
               );

				$this->session->set_userdata($socioLogueado);
			}
			else
			{
				echo "Passowrd incorrecta";
			}
			
		}
		redirect('', 'refresh');
	}
	
	public function Logout()
	{
		$this->session->sess_destroy();
		redirect('', 'refresh');
	}
	
	public function isAdmin()
	{
		if($this->session->userdata('admin') == 1) return true;
		else return false;
	}
	
	public function isLogged()
	{
		if($this->session->userdata('dni') == null) return false;
		return true;
	}
	
	public function redirectIfNotLogged()
	{
		if(!$this->isLogged) redirect('', 'refresh');
	}
	
	public function redirectIfNotAdmin()
	{
		if(!$this->isAdmin) redirect('', 'refresh');
	}

}
?>